package IOTWeb.com.handler;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.sql.Blob;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import IOTWeb.com.agreementclient.WebServer;
import IOTWeb.com.dao.ExcepContent;
import IOTWeb.com.utils.FileOption;
import IOTWeb.com.utils.LogSet;
import IOTWeb.com.utils.Utils;

public class DownloadHandler implements HttpHandler {
	
	@Override
	public void handle(HttpExchange exchange) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				Utils utils = new Utils();
				FileOption fileOption = new FileOption();
	            InputStream in = null;
				//获取url
	            LogSet.logger.info("url:" + exchange.getRequestURI().toString());
				// 获得查询字符串(get)
				String queryString = exchange.getRequestURI().getQuery();
				Map<String, String> filemap = fileOption.fileMap();
				Map<String, String> queryStringInfo = utils.formData2Dic(queryString);
				Headers responseHeaders = exchange.getResponseHeaders();
				//获取request头
				Headers requestHeaders = exchange.getRequestHeaders();
//				List<String> tocken = requestHeaders.get("token");
				try {
//					if(tocken != null && !tocken.isEmpty() && utils.haveTocken(tocken)) {
						String id = queryStringInfo.get("id");
						String type = queryStringInfo.get("type");
						Map<String,InputStream> fimap = fileOption.getFile(id);
					    for (Map.Entry<String,InputStream> entry : fimap.entrySet()) {
					        in = entry.getValue();
					    }
						ByteArrayOutputStream outStream = new ByteArrayOutputStream();
					    byte[] buffer = new byte[2048];
					    int len = 0;
					    while( (len=in.read(buffer)) != -1 ){
					        outStream.write(buffer, 0, len);
					    }
					    in.close();
					    byte data[] = outStream.toByteArray();
					    outStream.flush();
					    outStream.close();
					    if(type.equals("file"))
					    	utils.setResponseJs(responseHeaders, filemap.get("txt"));
					    else if(type.equals("img"))
					    	utils.setResponseJs(responseHeaders, filemap.get("img"));
					    else if(type.equals("video"))
					    	utils.setResponseJs(responseHeaders, filemap.get("video"));
					    else
					    	utils.setResponseJs(responseHeaders, filemap.get("else"));
					    len = data.length;
						exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, len);
					    OutputStream os = exchange.getResponseBody();
						PrintStream out = new PrintStream(os);
						out.write(data);
						out.flush();
						out.close();
						os.close();
//					}else {
//						utils.tockenError(exchange);
//					}
				} catch (Exception e) {
					e.printStackTrace();
					e.printStackTrace();
				}
			}
		}).start();
	}
}
